<template>
  <div>
    <el-form :inline="true" :model="formInline" class="demo-form-inline" @keyup.enter.native="search()">
      <el-form-item label="发放说明">
         <br> <br>
        <el-input v-model="formInline.sendRemark" placeholder style="margin-left:-60px;width:200px"></el-input>
      </el-form-item>
      <el-form-item label="发放时间">
         <br> <br>
        <el-date-picker
        
          v-model="formInline.sendDateBegin"
          type="date"
          value-format="yyyy-MM-dd"
        style="margin-left:-60px;width:200px"
        ></el-date-picker>至
        <el-date-picker
          v-model="formInline.sendDateEnd"
          type="date"
          value-format="yyyy-MM-dd"
         style="margin-left:10px;width:200px"
        ></el-date-picker>
      </el-form-item>
      <el-form-item style="margin-left:700px;text-align:right">
        <el-button type="primary" icon="el-icon-search" @click="search()" size="mini">查询</el-button>
      <!-- </el-form-item>
      <el-form-item> -->
        <el-button type="primary" @click="kong()" size="mini" icon="el-icon-delete">清空</el-button>
      <!-- </el-form-item>
      <el-form-item> -->
        <el-button type="primary" @click="toSend()" icon="el-icon-edit" size="mini">发放优惠券</el-button>
      </el-form-item>
    </el-form>

    <el-table
      :data="tableData"
      v-loading="loading"
      border
      height="400px"
      style="width: 100%;"
      :header-cell-style="{background:'#ebecf0',color:'#606266'}"
    >
      <el-table-column type="index" width="60" label="序号" align="center"></el-table-column>
      <el-table-column prop="sendDate" label="发放日期" header-align="center" width="150"></el-table-column>
      <el-table-column prop="sendRemark" label="发放说明" header-align="center"></el-table-column>
      <el-table-column
        prop="couponCount"
        label="卡券种类数"
        header-align="center"
        align="right"
        width="120"
      ></el-table-column>
      <el-table-column
        prop="memberCount"
        label="会员数"
        header-align="center"
        align="right"
        width="120"
      ></el-table-column>
      <el-table-column
        prop="couponQuantity"
        label="发送总张数"
        header-align="center"
        align="right"
        width="120"
      ></el-table-column>
      <el-table-column prop="createUser" label="发放人" header-align="center" width="180"></el-table-column>
      <!--<el-table-column prop="createTime" label="发放时间" align="center" width="180"></el-table-column>-->
      <el-table-column label="操作" width="180" align="center" header-align="center" fixed="right">
        <template slot-scope="scope">
          <el-button type="text" @click="sendDetail(scope.row)">发放卡券</el-button>
          <el-button type="text" @click="showSendMember(scope.row)">发放会员</el-button>
        </template>
      </el-table-column>
    </el-table>

    <div class="block" style="margin-top:5px;">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :page-sizes="[10, 20, 30, 50, 100]"
        :current-page="listQuery.current"
        :page-size="listQuery.size"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      ></el-pagination>
    </div>

    <el-dialog title="已发放的卡券" :visible.sync="dialogVisibleDetail" width="65%">
      <el-table
        :data="detailList"
        border
        style="font-size: 10px;width:100%;"
        :header-cell-style="{background:'#eef1f6',color:'#606266'}"
      >
        <el-table-column type="index" label="序号" align="center" ></el-table-column>
        <el-table-column
          prop="couponName"
          label="优惠券名称"
          header-align="center"
          width="220"
         
        ></el-table-column>
        <el-table-column prop="couponSeq" label="优惠券编号" header-align="center" width="150"></el-table-column>
        <el-table-column prop="couponMoney" label="优惠券面值" header-align="center" width="160">
          <template slot-scope="scope">
            <span>满{{scope.row.conponLimitMoney}}减{{scope.row.couponMoney}}元</span>
          </template>
        </el-table-column>
        <el-table-column prop="beginTime" label="有效期" header-align="center" width="180">
          <template slot-scope="scope">{{scope.row.beginTime}}至{{scope.row.endTime}}</template>
        </el-table-column>
        <el-table-column
          prop="memberCount"
          label="发放会员数"
          header-align="center"
          width="160"
          align="right"
        ></el-table-column>
        <el-table-column prop="sumQuantity" label="发放总张数" header-align="center" align="right"></el-table-column>
      </el-table>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisibleDetail = false">关 闭</el-button>
      </span>
    </el-dialog>

    <el-dialog title="已发放的会员" :visible.sync="dialogVisibleMember" width="75%">
      <el-table
        :data="memberList"
        border
        style="font-size: 10px;width:100%;"
        :header-cell-style="{background:'#eef1f6',color:'#606266'}"
      >
        <el-table-column type="index" label="序号" align="center" ></el-table-column>
        <el-table-column prop="memberInfo.userName" label="会员名" header-align="center" width="150"></el-table-column>
        <el-table-column prop="memberInfo.gradeLevel" label="会员等级" header-align="center" width="120"></el-table-column>
        <el-table-column prop="couponSeq" label="优惠券编号" header-align="center" width="150"></el-table-column>
        <el-table-column prop="couponName" label="优惠券名称" header-align="center"></el-table-column>
        <!--
        <el-table-column prop="couponMoney" label="优惠券面值" header-align="center" width="160">
          <template slot-scope="scope">
            <span>满{{scope.row.conponLimitMoney}}减{{scope.row.couponMoney}}元</span>
          </template>
        </el-table-column>
        -->
        <el-table-column prop="beginTime" label="有效期" header-align="center" width="180">
          <template slot-scope="scope">{{scope.row.beginTime}}至{{scope.row.endTime}}</template>
        </el-table-column>
        <el-table-column prop="status" label="使用状态" header-align="center" width="120">
          <template slot-scope="scope">
            <span v-if="scope.row.status == 1">未使用</span>
            <span v-if="scope.row.status == 2">已使用</span>
            <span v-if="scope.row.status == 3">已失效</span>
          </template>
        </el-table-column>
        <el-table-column prop="useCount" label="发放张数" header-align="center" width="120" align="right"></el-table-column>
      </el-table>
      <div class="block" style="margin-top:5px;">
        <el-pagination
          @size-change="memberSizeChange"
          @current-change="memberCurrentChange"
          :page-sizes="[10, 20, 30, 50, 100]"
          :current-page="listMember.current"
          :page-size="listMember.size"
          layout="total, sizes, prev, pager, next, jumper"
          :total="memberTotal"
        ></el-pagination>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisibleMember = false">关 闭</el-button>
      </span>
    </el-dialog>

  </div>
</template>

<script>
import {
  querySendRecord,
  querySendRecordDetail,
  querySendCouponMember
} from "@/api/promotionProgramEdit";

export default {
  data() {
    return {
      logininfo: {},
      orgList: [],
      orgSeq: "",
      loading: true,
      formInline: {
        sendRemark: "",
        sendDateBegin: "",
        sendDateEnd: ""
      },
      userId: "",
      total: 0,
      memberTotal: 0,
      listQuery: {
        current: 1,
        size: 100
      },
      listMember: {
        current: 1,
        size: 10
      },
      tableData: [],
      detailList: [],
      memberList: [],
      dialogVisibleDetail: false,
      dialogVisibleMember: false,
      curUuid: ""
    };
  },
  mounted() {
    this.logininfo = localStorage.getItem("logininfo");
    this.orgList = JSON.parse(this.logininfo).orgList;
    this.orgSeq = this.orgList[0].orgSeq;
    this.recordList();
  },
  methods: {
    kong() {
      this.formInline.sendRemark = "";
      this.formInline.sendDateBegin = "";
      this.formInline.sendDateEnd = "";
    },
    handleSizeChange(val) {
      this.listQuery.size = val;
      this.recordList();
    },
    handleCurrentChange(val) {
      this.listQuery.current = val;
      this.recordList();
    },
    search() {
      this.listQuery.current = 1;
      this.recordList();
    },
    recordList() {
      let data = {
        size: this.listQuery.size,
        current: this.listQuery.current,
        condition: {
          sendRemark: this.formInline.sendRemark,
          sendDateBegin: this.formInline.sendDateBegin,
          sendDateEnd: this.formInline.sendDateEnd
        },
        ascs: [],
        descs: []
      };
      querySendRecord(data).then(res => {
        this.loading = false;
        this.tableData = res.records;
        this.total = res.total;
      });
    },
    toSend() {
      this.$router.push({
        name: "sendCoupon",
        params: {}
      });
    },
    sendDetail(item) {
      var data = {
        size: 100,
        current: 1,
        condition: {
          recordUuid: item.uuid
        },
        ascs: [],
        descs: []
      };
      querySendRecordDetail(data).then(res => {
        this.detailList = res.records;
      });
      this.dialogVisibleDetail = !this.dialogVisibleDetail;
    }, 
    memberSizeChange(val) {
      this.listMember.size = val;
      this.sendMember();
    },
    memberCurrentChange(val) {
      this.listMember.current = val;
      this.sendMember();
    },
    showSendMember(item) {
      this.curUuid = item.uuid;
      this.sendMember();
      this.dialogVisibleMember = !this.dialogVisibleMember;
    },
    sendMember() {
      var data = {
        size: this.listMember.size,
        current: this.listMember.current,
        condition: {
          recordUuid: this.curUuid
        },
        ascs: [],
        descs: []
      };
      querySendCouponMember(data).then(res => {
        this.memberList = res.records;
        this.memberTotal = res.total;
      });
    }
  }
};
</script>
<style lang="less" scoped>
.toptitle {
  border: 1px solid #e0e0e0;
  border-bottom: 2px solid #188097;
  background: #f5f5f5;
  line-height: 35px;
  margin-bottom: 10px;
  padding: 0 10px;
  font-size: 14px;
  font-weight: bold;
}
.el-input--small .el-input__inner {
  height: 32px;
  line-height: 32px;
}
.demo-form-inline {
  //   border: 1px solid #ebeef5;
  padding: 20px 10px 0px 30px;
}
</style>